package com.sta3d.clientatm;


import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ClientATM extends BankomatMethods {

    public enum Keywords {
        ;
        public static final String USERLOGGEDOFF = "You were logged out";
        public static final String SELECT_MENU = "\nPlease, select the action: ... \n\nTo view balance enter               1\n\nTo withdraw from account enter      2\n\nTo exit press                       0\n";
        public static final String ENTER_SUM = "Please, enter the sum to withdraw in format NNNN, like 0051\n\n The maximum sum for 1 transaction should not be more than 9999\n";
        public static final String ERROR_OCCURED = "Error occured, please, try again";
        public static final String OUTPUT_INFO = "OUTPUT_INFORMATION_TO_SCREEN";
        public static final String ERROR_TRY_AGAIN = "ERROR, YOU WHERE LOGGED OUT, PLEASE TRY AGAIN ";
        public static final String INSTRUCTION_TO_SHOW = "SHOW";
        public static final String INSTRUCTION_TO_GET = "GETM";
        public static final String INPUT_PIN = "input PIN 1234 to use the device\n";
    }

    ;

    public static String userCard() {
        return "4769304941841937";
    }

    public static void main(String[] args) {
        while (true) {
            sayHello();
            choseOption();
        }
    }

    static void choseOption() {
        Scanner sc = new Scanner(System.in);
        int c = checkerCheck(sc);
        switch (c) {
            case 0:
                System.out.println(Keywords.USERLOGGEDOFF);
                System.exit(0);
                break;
            case 1:
                clearUserScreen();
                System.out.println(Keywords.SELECT_MENU);
                int action = checkerCheck(sc);
                choseInternalOption(action);
                break;
            case 2:
                System.out.println(Keywords.OUTPUT_INFO);
                ClientATM obInfo = new ClientATM();
                obInfo.getInfo();
                break;
            default:
                System.out.println(Keywords.ERROR_TRY_AGAIN);
                System.exit(0);
        }
    }

    static void choseInternalOption(int action) {
        switch (action) {
            case 0:
                System.exit(0);
                break;
            case 1:
                requestShowBalance();
                break;
            case 2:
                requestForTransaction();
                break;
            default:
                System.out.println(Keywords.ERROR_OCCURED);
                System.exit(0);
        }
    }

    private static int checkerCheck(Scanner sc) {
        try {
            return sc.nextInt();
        } catch (InputMismatchException ex) {
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }


    static void requestForTransaction() {
        clearUserScreen();
        Integer inputPin = getPin();
        String userData = userCard();
        String putcash = getCashString();
        ClientATM clientOn = new ClientATM();
        try {
            clientOn.connectToServerAndProvideTransaction(inputPin, putcash, Keywords.INSTRUCTION_TO_GET, userData);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getCashString() {
        System.out.println(Keywords.ENTER_SUM);
        Scanner scanner = new Scanner(System.in);
        String string;

        try {
            string = scanner.nextLine();
            int value = Integer.parseInt(string);
            if ((string.length() != 4) || (value >= 10000) || (value < 0)) {
                return "0000";
            }
        } catch (Exception e) {
            System.out.println(Keywords.ERROR_OCCURED);
            return "0000";
        }
        return string;
    }


    public static void requestShowBalance() {
        clearUserScreen();
        Integer inputPin = getPin();
        String userData = userCard();
        ClientATM clientOn = new ClientATM();
        try {
            clientOn.connectToServerAndProvideTransaction(inputPin, null, Keywords.INSTRUCTION_TO_SHOW, userData);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }


    static Integer getPin() {
        System.out.println(Keywords.INPUT_PIN);
        Scanner auth = new Scanner(System.in);
        try {
            return auth.nextInt();
        } catch (Exception e) {
            return 0;
        }
    }
}
